package cn.com.infohold.smartcity.view;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.support.v7.app.AlertDialog;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.TextView;

import cn.com.infohold.smartcity.R;
import library.image.ImageUtils;
import library.utils.DisplayUtil;

/**
 * Created by Mr.Fan on 2015/12/30.
 */
public class QuitDialog extends RunningDialog {
    private QuitDialog() {
    }

    public static void show(final Context context, final View.OnClickListener logout, final View.OnClickListener quit) {
        if (!(context instanceof Activity)) {
            return;
        }
        final AlertDialog dialog = new AlertDialog.Builder(context).create();
        instantiateDialog(dialog);
        dialog.show();
        Window window = dialog.getWindow();

        final int pad = DisplayUtil.dp2px(4);

        final LinearLayout linearLayout = new LinearLayout(context);
        final TextView tv_logout = getItem(context, R.mipmap.icon_quit_log, R.string.switch_account);
        final TextView tv_quit = getItem(context, R.mipmap.icon_quit_exit, R.string.quit_app);
        final View divider = new View(context);
        linearLayout.addView(tv_logout, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        linearLayout.addView(divider, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, DisplayUtil.dp2px(1)));
        linearLayout.addView(tv_quit, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        linearLayout.setPadding(pad, pad, pad, pad);
        linearLayout.setMinimumWidth(DisplayUtil.getMetrics().widthPixels * 3 / 5);
        linearLayout.setLayoutParams(new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT
        ));
        window.setContentView(linearLayout);

        linearLayout.setOrientation(LinearLayout.VERTICAL);
        tv_logout.setBackgroundResource(R.drawable.bg_touch_white);
        tv_quit.setBackgroundResource(R.drawable.bg_touch_white);
        divider.setBackgroundColor(Color.LTGRAY);

        tv_logout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                if (logout != null)
                    logout.onClick(v);
            }
        });
        tv_quit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                if (quit != null)
                    quit.onClick(v);
            }
        });
    }

    private static TextView getItem(Context context, int resId, int labelId) {
        final TextView tv = new TextView(context);
        final Bitmap bitmap = ImageUtils.obtain(context).resource(resId).load();
        final Drawable drawable = new BitmapDrawable(bitmap);
        tv.setTextSize(22);
        tv.setText(context.getString(labelId));
        tv.setGravity(Gravity.CENTER_VERTICAL);
        tv.setSingleLine(true);
        int dimen = (int) (tv.getTextSize() * 1.5);
        drawable.setBounds(0, 0, dimen, dimen);
        tv.setCompoundDrawablePadding(dimen / 2);
        tv.setCompoundDrawables(drawable, null, null, null);
        ViewGroup.MarginLayoutParams lp = new ViewGroup.MarginLayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT
        );
        int margin = DisplayUtil.dp2px(10);
        tv.setLayoutParams(lp);
        tv.setPadding(margin, margin, margin, margin);
        return tv;
    }

}
